//
//  GYDFoundViewModel.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/17.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

class GYDFoundViewModel: NSObject {
    weak private var foundVC : GYDFoundViewController?
    
    init(foundVC : GYDFoundViewController) {
        super.init()
        self.foundVC = foundVC
    }
    func startService() -> Void {
        let isLogin = GYDUserModel.sharedInstance.isLogin
        if isLogin {
            self.hiddenVisitorView()
        }else {
            self.showVisitorView()
        }
        GYDUserModel.sharedInstance.loginResultSignal.subscribeNext { [unowned self] (x) in
            if x as! String == LOGIN_SUCCESS {
                self.hiddenVisitorView()
            }else if x as! String == LOGINOUT_SUCCESS {
                self.showVisitorView()
            }
        }
    }
    func showVisitorView() -> Void {
        let loginBarItem = UIBarButtonItem(target: self, action: #selector(GYDFoundViewModel.registButtonClick), title: "注册", isLeft:true, titleColor: RGB(254, g: 118, b: 1))
        self.foundVC?.navigationItem.leftBarButtonItem = loginBarItem
        let registButton = UIBarButtonItem(target: self, action: #selector(GYDFoundViewModel.loginButtonClick), title: "登录", isLeft:false, titleColor: RGB(254, g: 118, b: 1))
        self.foundVC?.navigationItem.rightBarButtonItem = registButton
    }
    private func hiddenVisitorView() -> Void {
        let subViews = self.foundVC?.view.subviews
        for view in subViews! {
            view.removeFromSuperview()
        }
        self.foundVC?.navigationItem.leftBarButtonItem = nil
        self.foundVC?.navigationItem.rightBarButtonItem = nil
    }
    @objc func loginButtonClick() -> Void {
        let loginVC = GYDLoginViewController()
        self.foundVC?.presentViewController(loginVC, animated: true, completion: nil)
    }
    @objc func registButtonClick() -> Void {
        let loginVC = GYDLoginViewController()
        self.foundVC?.presentViewController(loginVC, animated: true, completion: nil)
    }
}
